<template>
  <div class="app-container">
    <div class="filter-container">
      <el-input :placeholder="$t('table.title')" v-model="listQuery.title" style="width: 200px;" class="filter-item" @keyup.enter.native="handleFilter"/>
      <el-select v-model="listQuery.status" placeholder="任务状态" clearable style="width: 90px" class="filter-item">
        <el-option v-for="item in allStatus" :key="item" :label="item" :value="item"/>
      </el-select>
      <el-select v-model="listQuery.publicer" placeholder="发布人" clearable class="filter-item" style="width: 130px">
        <el-option v-for="item in allPublicer" :key="item.id" :value="item.name"/>
      </el-select>
      <span style="display: inline-block">
        <el-date-picker
          v-model="listQuery.searchTime"
          :default-time="['00:00:00', '23:59:59']"
          type="datetimerange"
          align="right"
          start-placeholder="开始日期"
          end-placeholder="结束日期"/>
      </span>
      <!--<el-select v-model="listQuery.sort" style="width: 140px" class="filter-item" @change="handleFilter">-->
      <!--<el-option v-for="item in sortOptions" :key="item.key" :label="item.label" :value="item.key"/>-->
      <!--</el-select>-->
      <el-button v-waves class="filter-item" type="primary" icon="el-icon-search" @click="handleFilter">{{ $t('table.search') }}</el-button>

    </div>
    <el-table v-loading.body="listLoading" :data="list" border fit highlight-current-row style="width: 100%">
      <el-table-column align="center" label="ID" width="80">
        <template slot-scope="scope">
          <span>{{ scope.row.id }}</span>
        </template>
      </el-table-column>

      <el-table-column min-width="100px" label="标题(点击单元格查看详情)">
        <template slot-scope="scope">
          <div style="cursor:pointer;color: #3A71A8" @click="showTaskDetailDialog(scope.row.content)">{{ scope.row.title }}</div>
        </template>
      </el-table-column>

      <el-table-column label="发布人">
        <template slot-scope="scope">
          <span>{{ scope.row.publicerName }}</span>
        </template>
      </el-table-column>
      <el-table-column label="接收人">
        <template slot-scope="scope">
          <span>{{ scope.row.receiverName }}</span>
        </template>
      </el-table-column>

      <el-table-column min-width="100px" label="创建时间">
        <template slot-scope="scope">
          <span>{{ scope.row.createTime }}</span>
        </template>
      </el-table-column>

      <el-table-column min-width="100px" label="修改时间">
        <template slot-scope="scope">
          <span>{{ scope.row.updateTime }}</span>
        </template>
      </el-table-column>

      <el-table-column class-name="status-col" label="状态" width="110">
        <template slot-scope="scope">
          <el-tag :type="scope.row.status | statusFilter">{{ scope.row.status }}</el-tag>
        </template>
      </el-table-column>

      <el-table-column class-name="status-col" label="解决方案" width="110">
        <template slot-scope="scope">
          <div @click="showSloveDetailDialog(scope.row)">
            <el-button type="primary">查看</el-button>
          </div>
        </template>
      </el-table-column>
    </el-table>

    <div class="pagination-container">
      <el-pagination
        :current-page="listQuery.page"
        :page-sizes="[10,20,30, 50]"
        :page-size="listQuery.limit"
        :total="total"
        background
        layout="total, sizes, prev, pager, next, jumper"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"/>
    </div>

    <el-dialog :title="textMap[dialogStatus]" :visible.sync="dialogFormVisible">
      <el-form ref="dataForm" :rules="rules" :model="temp" label-position="left" label-width="100px" style="width: 400px; margin-left:50px;">
        <el-form-item label="工程名" prop="projectName">
          <el-input v-model="temp.projectName"/>
        </el-form-item>
        <el-form-item label="SQL" prop="sloveSql">
          <el-input
            v-model="temp.sloveSql"
            type="textarea"
            rows="10"/>
        </el-form-item>
        <el-form-item label="分钟" prop="minutes">
          <el-input v-model="temp.minutes"/>
        </el-form-item>
        <el-form-item label="小时" prop="hour">
          <el-input v-model="temp.hour"/>
        </el-form-item>
        <el-form-item label="每周第几天" prop="day">
          <el-input v-model="temp.day"/>
        </el-form-item>
        <el-form-item label="每月第几天" prop="month">
          <el-input v-model="temp.month"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取消</el-button>
        <el-button type="primary" @click="saveData">确定</el-button>
      </div>
    </el-dialog>

    <!--任务详情-->
    <el-dialog :visible.sync="dialogShowTaskDetailVisible" title="详情" width="60%">
      <span>{{ rowContent }}</span>
      <span slot="footer" class="dialog-footer">
        <el-button type="primary" @click="dialogShowTaskDetailVisible = false">确定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { getAcceptedListForReceivers, saveByReceiver } from '@/api/task'

export default {
  name: 'MyTasks',
  filters: {
    statusFilter(status) {
      return 'ooooo'
    }
  },
  data() {
    return {
      // 筛选
      allStatus: { '已接受未完成': '已接受未完成', '已完成': '已完成' },
      allPublicer: [{ id: '1', name: 'jython' }, { id: '1', name: 'hailin' }],
      listQuery: {
        page: 1,
        limit: 20,
        status: undefined,
        publicer: undefined,
        searchTime: ''
      },
      dialogStatus: '',
      textMap: {
        update: 'Edit',
        create: 'Create'
      },
      temp: {
        id: undefined,
        projectName: 'test',
        sloveSql: 'select * from ',
        crontabExpression: '* * * * *',
        minutes: '*',
        hour: '*',
        day: '*',
        month: '*'
      },
      rules: {
        type: [{ required: true, message: 'type is required', trigger: 'change' }],
        timestamp: [{ type: 'date', required: true, message: 'timestamp is required', trigger: 'change' }],
        title: [{ required: true, message: 'title is required', trigger: 'blur' }]
      },
      dialogShowTaskDetailVisible: false,
      dialogFormVisible: false,
      rowContent: '',
      isGet: true,
      list: null,
      total: 0,
      listLoading: true,
      listQuery: {
        publicer_dd_id: '',
        receiver_dd_id: '',
        status: '',
        start_time: '',
        end_time: '',
        page: 1,
        limit: 10
      }
    }
  },
  created() {
    this.getList()
  },
  methods: {
    getList() {
      this.listLoading = true
      getAcceptedListForReceivers(this.listQuery).then(response => {
        this.list = response.data.rows
        this.total = response.data.total
        this.listLoading = false
      })
    },
    showSloveDetailDialog() {
      this.dialogFormVisible = true
    },
    showTaskDetailDialog(content) {
      this.rowContent = content
      this.dialogShowTaskDetailVisible = true
    },
    saveData() {
      this.listLoading = true
      const th = this
      saveByReceiver(this.temp).then(response => {
        console.log(response)
        th.$notify({
          title: '成功',
          message: '保存成功',
          type: 'success',
          duration: 2000
        })
        th.listLoading = false
      })
    },
    handleSizeChange(val) {
      this.listQuery.limit = val
      this.getList()
    },
    handleCurrentChange(val) {
      console.log(val)
      this.listQuery.page = val
      this.getList()
    }
  }
}
</script>

<style scoped>

</style>
